#!/usr/bin/env python
#coding=utf-8

#
# Min Feng 02/14/08
#
#

import dbf
import os
import re
import httplib
import sys

class dayMet(object):
	def __init__(self):
		self.con = None
	
	def getConnection(self):
		self.con = httplib.HTTPConnection('www.daymet.org')
		
	def request(self, url):
		if not self.con:
			self.getConnection()
		
		for i in range(10):
			print '\turl', url, i
			try:
				self.con.request('GET', url)
				_response = self.con.getresponse()
			
				return _response
			except Exception, err:
				print '\terror', err
				self.close()
				self.getConnection()
		
	def writeDayMet(self, f, r):
		_name = ["year","day","Tmax","Tmin","Tday","prcp","VPD","srad","daylen"]
		_type = [('N',4,0),('N',4,0),('N',12,2),('N',12,2),('N',12,2),('N',12,2),('N',12,2),('N',12,2),('N',12,2)]
		
		_r = []
		for _l in r.read().split('\n'):
			_m = re.match('\s*([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s+([0-9\.-]+)\s*', _l)
			if _m:
				_r.append(_m.groups())
		
		dbf.dbfwriter(f, _name, _type, _r)
		
	def downloadDayMet(self, f, x, y):
		_url = '/getRawData.do?lat=%f&lon=%f' % (y, x)
		
		print 'download', _url
		_response = self.request(_url)
		if _response.status == 200:
			self.writeDayMet(f, _response)
#			f.write(_response.read())
			print 'success'
		else:
			print 'error', _response.status, _response.reason
	
	def close(self):
		print 'close connection ...',
		if self.con:
			try:
				self.con.close()
				print 'ok'
			except Exception, err:
				print err
		else:
			print 'no connection'

if __name__ == '__main__':
	if len(sys.argv) != 3:
		print """Download DayMet v 0.1 02/14/2008
Usage:
	download.daymet.py [input shapefile] [output folder]
		"""
	else:
		_inputFile = sys.argv[1]
		_outputPath = sys.argv[2]
		
		if _inputFile.endswith('.shp'):
			_inputFile = _inputFile[:-4] + '.dbf'
			
		print ""
		print 'input shapefile dbf', _inputFile
		print 'output folder', _outputPath
	
#	_inputFile = 'd:\\mfeng\\save\\java\\wps\\data\\fire\\output1\\out.dbf'
#	_outputPath = 'D:\\mfeng\\save\\java\\wps\\data\\fire\\daymet'
	
		_day = dayMet()
		_dbf = dbf.dbfreader(file(_inputFile, 'rb'))
		
		_name = _dbf.next()
		_type = _dbf.next()
		
		_recs = list(_dbf)
		
		print ''
		print 'downloading starts'
		print '***************************************'
		
		_pos = 0
		for _r in _recs:
			print str(_pos + 1) + '/' + str(len(_recs)), _r[_name.index('id')].strip(), 'lon=' + str(float(_r[_name.index('lon')])), 'lat=' + str(float(_r[_name.index('lat')]))
			
			_f = file(os.path.join(_outputPath, _r[_name.index('id')].strip() + '.dbf'), 'wb')
			try:
				_day.downloadDayMet(_f, float(_r[_name.index('lon')]), float(_r[_name.index('lat')]))
			except Exception, err:
				print 'download failed', err
			
			_f.flush()
			_f.close()
			
			_pos += 1
			
			print '---------------------------------------'
		
		_day.close()
